<!DOCTYPE HTML>
<html>
<head>
    <title>积分商城</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <script src="/scripts/boot.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <link rel="stylesheet" href="/editor/common.css">
    <script src="/scripts/miniui/miniui.js"></script>
</head>
<body  style="padding: 0;width: 100%;height: 97%">
<div style="width:99%;height: 97%;">
    <div id="outImg">
        <img id="imgId" src="" alt="" onclick="smallImg()">
    </div>
    <div style="width:99%;">
        <div style="padding: 5px">
            <span>商品分类：</span><input  class="mini-combobox" type="text" id="goodsParentType" data="goodsParentType" onvaluechanged="search"/>
            <span>商品名称：</span><input  class="mini-textbox" type="text" id="goodsName" />
            <a class="mini-button" iconCls="icon-search" onclick="search()">查找</a>
        </div>
        <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" iconCls="icon-add" onclick="intergationAdd()">增加</a>
                        <a class="mini-button" iconCls="icon-edit" onclick="intergationEdit()">设置</a>
                        <a class="mini-button" iconCls="icon-remove" onclick="intergationDelete()">刪除</a>
                        <a id="moveUp" class="mini-button" iconCls="icon-edit" onclick="move(this)">排序上移</a>
                        <a id="moveDown" class="mini-button" iconCls="icon-edit" onclick="move(this)">排序下移</a>
                        <a id="moveDown" class="mini-button" iconCls="icon-edit" onclick="moveTop()">同分类置顶</a>
                        <a id="batchAdd" class="mini-button" iconCls="icon-add" onclick="batchAdd()">批量添加站外商品</a>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div id="datagrid1" class="mini-datagrid" style="width:99%;height: 97%;" multiSelect="true"
         url="/getdata/shopIntergation" idField="id" allowResize="true"
         sizeList="[20,30,50,100]" pageSize="20">
        <div property="columns">
            <div type="checkcolumn" width="20" ></div>
            <div field="id" width="80" headerAlign="center" allowSort="true" >积分商城Id</div>
            <div field="objectId" width="80" headerAlign="center" allowSort="true" >商品Id</div>
            <div field="title" width="80" headerAlign="center" align="center" allowSort="true" >商品名称</div>
            <div field="classify" width="80" headerAlign="center" align="center" allowSort="true" >商品类型</div>
            <div field="goodsType" width="80" headerAlign="center" align="center" allowSort="true" >商品分类</div>
            <div field="shopGoodsTypeParentOId" width="0" headerAlign="center" align="center" allowSort="true" style="display: none">商品分类PID</div>
            <div field="isVip" width="80" renderer="onVipRenderer" headerAlign="center" align="center" allowSort="true" >是否是VIP商品</div>
            <div field="sortNum" width="60" align="center" allowSort="true">排序编号</div>
            <div field="shopIcon"  width="70" headerAlign="center" align="center" headerAlign="center" renderer="onGenderRenderer" allowSort="true" >商品Icon</div>
            <div field="name" width="80" headerAlign="center" align="center" allowSort="true" >店铺名称</div>
            <div field="price" width="50" headerAlign="center" align="center" allowSort="true" >价格</div>
            <div field="freight" width="50" headerAlign="center" align="center" allowSort="true">运费</div>
            <div field="score" width="50" headerAlign="center" align="center" allowSort="true">积分</div>
            <div field="count" width="50" headerAlign="center" align="center" allowSort="true">购买数量</div>
            <div field="presentScore" width="60" align="center" allowSort="true">购买后赠送积分</div>
            <div field="brokerage" width="60" align="center" allowSort="true">佣金总额</div>
            <div field="superiorMoney" width="60" align="center" allowSort="true">上一级分成钱</div>
            <div field="superiorSuperiorMoney" width="60" align="center" allowSort="true">上二级分成钱</div>
            <div field="superiorSuperiorSuperiorMoney" width="60" align="center" allowSort="true">上三级分成钱</div>
            <div field="ancestorsMoney" width="60" align="center" allowSort="true">上三级祖辈分成钱</div>
            <div field="superiorScore" width="60" align="center" allowSort="true">上一级分成积分比例</div>
            <div field="superiorSuperiorScore" width="60" align="center" allowSort="true">上二级分成积分比例</div>
            <!--<div field="superiorSuperiorSuperiorScore" width="60" align="center" allowSort="true">上三级分成积分比例</div>-->
            <div field="ancestorsScore" width="60" align="center" allowSort="true">上二级祖辈分成积分比例</div>
            <div field="createdAt" Swidth="100" renderer="onBirthdayRenderer"   align="center" headerAlign="center" allowSort="true">创建日期</div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
  var goodsParentType = JSON.parse('<%- goodsParentType %>');
  var goodsParentTypeID = ''
  var flag = 1;
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load();
    grid.on("drawcell", function (e) {
        var value = e.value;
        var column = e.column;
        if (column.field == "shopIcon") {
            if(value) {
                e.cellHtml = '<img class="bigImg" onclick="bigImg()" src="' + value + '" width="30" height="30" alt="图片">';
            }else{
                e.cellHtml = '<img class="bigImg" onclick="bigImg()"  src="http://7xiuo5.com1.z0.glb.clouddn.com/fgImage.png?imageMogr2/auto-orient/thumbnail/!100x100r/gravity/Center/crop/100x100/quality/80/auto-orient" width="30" height="30" alt="图片">';
            }
        }
    });

    function bigImg() {
        var row = grid.getSelected();
        if (row) {
            if(flag==1) {
                var path = row.shopIcon;
                $("#imgId").attr('src', path);
                $("#outImg").css('display','block');
            }
        }
    }
    function smallImg() {
        $("#outImg").css('display','none');
    }

    function onBirthdayRenderer(e) {
        var value = e.value;
        if (value) return mini.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
        return "";
    }
  function onVipRenderer(e) {
      var value = e.value;
      if(value==0){
          return "否";
      }
      return "是";
  }


    function onSelectedChanged(e) {
        var shopIntergation = e.sender;
        var record = shopIntergation.getSelected();
        if (record) {
            objectId = record.id;
        }
    };

    function  intergationAdd() {
        mini.open({
            url: "/getdata/intergationAdd",
            title: "增加商品", width: 600, height: 500,
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }

    function intergationEdit() {
       var rows = grid.getSelecteds();
       if(rows.length>1){
           alert("设置按钮只能选择一条记录");
       }else{
        var row = grid.getSelected();
        if(row){
            mini.open({
                url:'/getdata/scoreGoodsEdit?objectId='+row.id,
                title: "编辑", width: 450, height: 450,
                ondestroy: function (action) {
                    grid.reload();
                }
            })
        }else{
            alert("请选中一条记录")
        }
    }
    }

    function  intergationDelete() {
        var arrRow = grid.getSelecteds();
        var  ids = [];
        for(var i = 0;i<arrRow.length;i++){
            ids.push(arrRow[i].id);
        }
        if(ids.length>=1){
            $.ajax({
                url:'/getdata/deleteShopIntergation',
                type:'POST',
                data:{
                    ids
                },
                success:function (result) {
                    if(result=="10001"){
                        alert("删除成功");
                        grid.reload();
                    }else{
                        alert("删除失败");
                    }
                }
            })
        }else {
            alert("请选中一条记录");
        }
    }
    function search() {
      let goodsName = mini.get("goodsName").getValue();
      if (!goodsName){
        goodsName = ''
      }
      goodsParentTypeID = mini.get("goodsParentType").getValue();
      if (!goodsParentTypeID){
        goodsParentTypeID = ''
      }
      grid.load({
        goodsName:goodsName,
        goodsParentTypeID:goodsParentTypeID
      });
    }
    function bigImg() {
      var row = grid.getSelected();
      if (row) {
        if(flag==1) {
          var path = row.shopIcon;
          $("#imgId").attr('src', path);
          $("#outImg").css('display','block');
        }
      }
    }
    function  move(e) {
      if (goodsParentTypeID == ''){
        alert('请先查找分类之后排序！');
        return
      }
      let buttonId = e.id
      var row = grid.getSelected();
      if(row){
        let fromId = row.id
        let shopGoodsTypeParentOId = row.shopGoodsTypeParentOId
        // let fromClassify = row.goodsType
        let fromSort = row.sortNum
        let index = grid.indexOf(row)
        let toRowDic = {}
        if (buttonId == 'moveUp'){
          toRowDic = getMoveUpRowObjFromIndex(index,shopGoodsTypeParentOId)
        } else if (buttonId == 'moveDown'){
          toRowDic = getMoveDownRowObjFromIndex(index,shopGoodsTypeParentOId)
        } else {
          alert('非排序操作！')
          return
        }
        if (toRowDic){
          let toId = toRowDic.id
          let toSort = toRowDic.sortNum
          $.ajax({
            url:'/getdata/moveScoreShopGoods',
            type:'POST',
            data:{
              fromId:fromId,
              fromSort:fromSort,
              toId:toId,
              toSort:toSort
            },
            success:function (result) {
              if(result == '10000'){
                grid.reload();
              }else{
                alert("移动失败!");
                return
              }
            }
          })
        } else {
          if (buttonId == 'moveUp'){
            alert("未找到本分类中更高的排序！");
            return

          } else if (buttonId == 'moveDown'){
            alert("未找到本分类中更低的排序！");
            return
          } else {
            alert('非排序操作！')
            return
          }

        }
      }else {
        alert("请选中一条记录");
        return
      }
    }
    function getMoveUpRowObjFromIndex(index,shopGoodsTypeParentOId) {
      if (index==0){
        return false
      }
      let targetRow = grid.getRow(index-1)

      if (targetRow.shopGoodsTypeParentOId == shopGoodsTypeParentOId){

        return {id:targetRow.id,sortNum:targetRow.sortNum}
      } else {
        return getMoveUpRowObjFromIndex(index-1,shopGoodsTypeParentOId)
      }
    }
    function getMoveDownRowObjFromIndex(index,shopGoodsTypeParentOId) {
      if (index==grid.data.length-1){
        return false
      }
      let targetRow = grid.getRow(index+1)

      if (targetRow.shopGoodsTypeParentOId == shopGoodsTypeParentOId){

        return {id:targetRow.id,sortNum:targetRow.sortNum}
      } else {
        return getMoveDownRowObjFromIndex(index+1,shopGoodsTypeParentOId)
      }
    }
    function moveTop() {
      if (goodsParentTypeID == '') {
        alert('请先查找分类之后排序！');
        return
      }
      var row = grid.getSelected();
      if (row) {
        let index = grid.indexOf(row)
        if (index == 0) {
          alert('已经是本分类置顶！');
          return
        }
        // let fromId = row.id
        // let shopGoodsTypeParentOId = row.shopGoodsTypeParentOId
        // let fromClassify = row.goodsType
        // let fromSort = row.sortNum
        let targetDic = getMoveTopRowObjFromIndex(index,row.shopGoodsTypeParentOId)
        if (targetDic) {
          $.ajax({
            url: '/getdata/moveTopScoreShopGoods',
            type: 'POST',
            data: targetDic,
            success: function (result) {
              if (result == '10000') {
                grid.reload();
              } else {
                alert("置顶失败！");
                return
              }
            }
          })
        } else {
          alert("数据错误，请刷新重试！");
          return
        }

      }
    }
  function getMoveTopRowObjFromIndex(index,shopGoodsTypeParentOId) {
      let sortArr = []
      let idArr = []
      let changeCount = index+1
      for (let i = 0; i<changeCount; i++){
        let targetRow = grid.getRow(i)
        if (targetRow.shopGoodsTypeParentOId == shopGoodsTypeParentOId){
          idArr.push(targetRow.id)
          sortArr.push(targetRow.sortNum)
        }
      }
      let lastIdItem = idArr.pop()
      idArr.unshift(lastIdItem)
      return {targetIdArr:idArr,targetSortNumArr:sortArr}
  }
  function batchAdd() {
    mini.open({
      url: "/getdata/shopIntergationBatchAdd",
      title: "批量添加站外商品", width: 600, height: 500,
      ondestroy: function (action) {
        grid.reload();
      }
    });
  }
</script>
</html>
